package com.boxuegu.guanweihua25.boxuegudatabinding.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.boxuegu.guanweihua25.boxuegudatabinding.MainActivity;
import com.boxuegu.guanweihua25.boxuegudatabinding.R;
import com.boxuegu.guanweihua25.boxuegudatabinding.apis.Apis;
import com.boxuegu.guanweihua25.boxuegudatabinding.databinding.ActivityLoginBinding;
import com.boxuegu.guanweihua25.boxuegudatabinding.model.LoginModel;
import com.boxuegu.guanweihua25.boxuegudatabinding.utils.RetrofitUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    ObservableField<String>username=new ObservableField<>();
    ObservableField<String>password=new ObservableField<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityLoginBinding binding= DataBindingUtil.setContentView(this,R.layout.activity_login);
        binding.setUsername(username);
        binding.setPassword(password);
//        setContentView(R.layout.activity_login);
        binding.setMyclick(new MyClick());
        binding.text3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });



    }
    public class MyClick{
        public void onClick(View view){
            final Call<LoginModel> loginModelCall= RetrofitUtils.getRetrofit().create(Apis.class)
                    .getLogin(username.get(),password.get());
            if (TextUtils.isEmpty(username.get())){
                Toast.makeText(LoginActivity.this,"请输入用户名",Toast.LENGTH_LONG).show();
                return;
            }
            if (TextUtils.isEmpty(password.get())){
                Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_LONG).show();
                return;
            }
            loginModelCall.enqueue(new Callback<LoginModel>() {
                @Override
                public void onResponse(Call<LoginModel> call, Response<LoginModel> response) {
                    LoginModel loginModel=response.body();
                    if(response.body().code ==0){
                        //跳主页面
                        Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    }else{
                        //弹出toast提示
                        Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();
                    }

                }


                @Override
                public void onFailure(Call<LoginModel> call, Throwable t) {
//                    Log.i("LoginActivity","t"+t);
                }
            });
        }
    }
}
